import { ElMessage } from "element-plus"

function fallbackCopy(text) {
  const textarea = document.createElement('textarea');
  textarea.value = text;
  textarea.style.position = 'fixed'; // 避免触发滚动
  textarea.style.top = '-9999px';
  document.body.appendChild(textarea);
  textarea.select();

  try {
    const success = document.execCommand('copy');
    if (success) {
      ElMessage.success('copy success');
    } else {
      ElMessage.error('copy failed');
    }
  } catch (err) {
    ElMessage.error('copy failed');
  } finally {
    document.body.removeChild(textarea);
  }
}

export async function copyToClipboard(text) {
  try {
    await navigator.clipboard.writeText(text);
    ElMessage.success('copy success');
  } catch (err) {
    fallbackCopy(text);
  }
}

export function getDeviceOS() {
  const ua = navigator.userAgent;

  // 检测 iOS
  const isIOS = /iPhone|iPad|iPod/i.test(ua) || 
              (navigator.platform === 'MacIntel' && navigator.maxTouchPoints > 1);
  // 检测 Android
  const isAndroid = /Android/i.test(ua);

  return isIOS ? 'ios' : isAndroid ? 'android' : 'unknown';
}
